package com.yuda.campsite.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.yuda.campsite.domain.RegisteredUser;
import com.yuda.campsite.service.RegisteredUserService;
import com.yuda.common.core.utils.file.OssFileUtils;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.web.controller.BaseController;
import com.yuda.common.core.web.domain.AjaxResult;
import com.yuda.common.core.web.domain.BaseEntity;
import com.yuda.common.datascope.annotation.DataScope;
import io.swagger.models.auth.In;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: xiaohudie
 * @Date: 2021/11/24 10:28
 * @Version: 1.8
 */
@RestController
@RequestMapping("/regis")
public class registeredUserController extends BaseController {
    /**
     * 自动注入oss这个类
     */
    @Resource
    private OSS oss;
   @Resource
    private RegisteredUserService registeredUserService;

//   @DataScope(deptAlias = "")
   @GetMapping("/regisView")
    public String regisView(BaseEntity baseEntity,
                            @RequestParam(value = "provinceName" ,required = false) String provinceName,
                            @RequestParam(value = "cityName",required = false) String cityName,
                            @RequestParam(value = "studentName",required = false)String studentName,
                            @RequestParam(value = "schoolName",required = false)String schoolName,
                            @RequestParam(value ="qisi",defaultValue = "1")Integer qisi,
                            @RequestParam(value = "offset",defaultValue = "10")Integer offset){

       int size = registeredUserService.getCount(baseEntity.getParams(),provinceName, cityName, studentName, schoolName);
       List<RegisteredUser> registeredUsers = registeredUserService.registerView(baseEntity.getParams(),provinceName, cityName, studentName, schoolName, (qisi-1)*offset ,offset);
       for (RegisteredUser registeredUser:registeredUsers){
           if (registeredUser.getGender()==null || registeredUser.getGender().equals("2")){
               registeredUser.setGender("未填写");
           }if (registeredUser.getGender()!=null && registeredUser.getGender().equals("0")){
               registeredUser.setGender("男");
           }if (registeredUser.getGender()!=null && registeredUser.getGender().equals("1")){
               registeredUser.setGender("女");
           }
           if (registeredUser.getIsDisable()!=null && registeredUser.getIsDisable().equals("0")){
               registeredUser.setIsDisable("正常");
           }
           if (registeredUser.getIsDisable()==null || registeredUser.getIsDisable().equals("1")){
               registeredUser.setIsDisable("禁用");
           }
       }
       JSONObject jsonObject = new JSONObject();
       jsonObject.put("msg","请求成功");
       jsonObject.put("total",size);
       jsonObject.put("code",200);
       jsonObject.put("data",registeredUsers);
      return jsonObject.toJSONString();
   }
   /**实现多张学习资料审核的上传到OSS，并生成url，存储到数据库中*/
   @PostMapping("/manyPicture")
    public AjaxResult manyPicture(MultipartFile[] multipartFiles,String campsiteId,String idCard){
       /**创建一个Linked集合是为了方便插入URL*/
       List<String> lits =new LinkedList<>();
       /**判断文件数组是否为空*/
       if (multipartFiles==null ||multipartFiles.length<=0){
           return AjaxResult.error("上传的文件为空");
       }
       /**判断文件数组的大小不能超过10个*/
       if (multipartFiles.length>10){
           return AjaxResult.error("文件数超过了10个");
       }
       /**遍历得到各个文件对象*/
       for (int i =0;i<multipartFiles.length;i++){
           MultipartFile multipartFile = multipartFiles[i];
           if (multipartFile.getSize()>=10485760){
               return AjaxResult.error("第" + i +"张图片大小超过了10M了，请重新上传");
           }
           /**开始上传文件了*/
           String fileName = OssFileUtils.generateFileName(multipartFile);
           String url = null;
           String objectName = "png/" + fileName;
           String bucketName = "campisteimage";
           /**判断文件是否符合上传规范*/
      if (fileName.endsWith(".jpg")||fileName.endsWith(".jpeg")||fileName.endsWith(".png")){
          try {
              oss.putObject(bucketName,objectName,new ByteArrayInputStream(multipartFile.getBytes()));
              url = "http://campisteimage.oss-cn-beijing.aliyuncs.com" + "/" + objectName;
              lits.add(url);
          } catch (IOException e) {
              e.printStackTrace();
              System.out.println("文件上传失败");
          }
      }else {
          return AjaxResult.error("上传的文件格式不正确请重新上传");
      }
       }
       /**将所得的URL集合转换为大的字符串*/
       String join = StringUtils.join(lits, ",");
       int i = registeredUserService.shengheUrl(join, campsiteId, idCard);
       return AjaxResult.success("修改成功",lits);
   }
   /**导出注册用户全量表*/
//   @DataScope(deptAlias = "")
   @PostMapping("/ZhuCeExport")
    public void ZhuCeExport(BaseEntity baseEntity,HttpServletResponse httpServletResponse){
       List<RegisteredUser> registeredUsers = registeredUserService.registerView(baseEntity.getParams(),null, null, null, null, 0, 10000);
       for (RegisteredUser registeredUser:registeredUsers){
           if (registeredUser.getGender()!=null && registeredUser.getGender().equals("0")){
               registeredUser.setGender("男");
           }if (registeredUser.getGender()==null ||registeredUser.getGender().equals("1")){
               registeredUser.setGender("女");
           }if (registeredUser.getGender()!=null && registeredUser.getGender().equals("2")){
               registeredUser.setGender("保密");
           }
           if (registeredUser.getIsDisable()!=null && registeredUser.getIsDisable().equals("0")){
               registeredUser.setIsDisable("正常");
           }if (registeredUser.getIsDisable()==null || registeredUser.getIsDisable().equals("1")){
               registeredUser.setIsDisable("禁用");
           }
       }
       ExcelUtil<RegisteredUser> userExcelUtil =new ExcelUtil<>(RegisteredUser.class);
       try {
           userExcelUtil.exportExcel(httpServletResponse,registeredUsers,"ZhuCe");
       } catch (IOException e) {
           e.printStackTrace();
           System.out.println("导出Excel失败");
       }
   }
   /**将学生账号进行封禁*/
   @GetMapping("FengHaoLe")
    public AjaxResult FengHaoLe(@RequestParam(name = "isDisable") String isDisable,
                                @RequestParam(name = "phone") String phone){
       int i = registeredUserService.FenHao(isDisable, phone);
       return AjaxResult.success("修改成功");
   }
   /**查询学生是否被封了号*/
   @GetMapping("isFengHao")
    public AjaxResult isFengHao(@RequestParam(name = "phone") String phone){
       String fenhao = registeredUserService.isFenhao(phone);
       return AjaxResult.success("得到账号状态啦(0是正常，1是禁用)",fenhao);
   }
}
